using MauiBuilder.Themes;

namespace MauiBuilder.Services
{
    /// <summary>
    /// 主题管理服务
    /// </summary>
    public class ThemeManager
    {
        private static ThemeManager _instance;
        public static ThemeManager Instance => _instance ??= new ThemeManager();

        /// <summary>
        /// 主题更改事件
        /// </summary>
        public event Action<bool> ThemeChanged;

        private ThemeManager()
        {
        }

        /// <summary>
        /// 切换主题
        /// </summary>
        /// <param name="isDark">是否为深色主题</param>
        public void SwitchTheme(bool isDark)
        {
            try
            {
                var currentApp = Application.Current;
                if (currentApp == null) return;

                // 清除现有的主题资源
                var existingLightTheme = currentApp.Resources.MergedDictionaries.FirstOrDefault(d => d.GetType() == typeof(LightTheme));
                var existingDarkTheme = currentApp.Resources.MergedDictionaries.FirstOrDefault(d => d.GetType() == typeof(DarkTheme));

                if (existingLightTheme != null)
                    currentApp.Resources.MergedDictionaries.Remove(existingLightTheme);

                if (existingDarkTheme != null)
                    currentApp.Resources.MergedDictionaries.Remove(existingDarkTheme);

                // 添加新主题
                if (isDark)
                {
                    currentApp.Resources.MergedDictionaries.Add(new DarkTheme());
                }
                else
                {
                    currentApp.Resources.MergedDictionaries.Add(new LightTheme());
                }

                // 设置应用主题
                currentApp.UserAppTheme = isDark ? AppTheme.Dark : AppTheme.Light;
                
                // 触发主题更改事件
                ThemeChanged?.Invoke(isDark);
                
                // 保存主题设置
                SettingsManager.Instance.SaveSetting(SettingsManager.DarkModeKey, isDark);
            }
            catch (Exception ex)
            {
                // 记录异常但不中断应用
                System.Diagnostics.Debug.WriteLine($"切换主题时发生错误: {ex.Message}");
            }
        }

        /// <summary>
        /// 获取当前是否为深色主题
        /// </summary>
        /// <returns></returns>
        public bool IsDarkTheme()
        {
            return Application.Current?.UserAppTheme == AppTheme.Dark ||
                   (Application.Current?.UserAppTheme == AppTheme.Unspecified &&
                    Application.Current?.RequestedTheme == AppTheme.Dark);
        }
        
        /// <summary>
        /// 加载保存的主题设置
        /// </summary>
        public void LoadSavedTheme()
        {
            try
            {
                // 从设置中加载主题
                bool isDarkTheme = SettingsManager.Instance.LoadSetting(SettingsManager.DarkModeKey, false);
                SwitchTheme(isDarkTheme);
            }
            catch (Exception ex)
            {
                // 如果加载失败，使用默认主题
                System.Diagnostics.Debug.WriteLine($"加载主题设置时发生错误: {ex.Message}");
                SwitchTheme(false);
            }
        }
    }
}